package it.unibo.refolding.pico.mfp;

import it.unibo.refolding.common.CommonThings;

import java.util.Objects;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class Checksummed<T> {

  public final T object;
  public final short unsignedChecksum;

  public Checksummed(T object, short unsignedChecksum) {
    this.object = object;
    this.unsignedChecksum = unsignedChecksum;
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof Checksummed<?>)) {
      return false;
    }
    Checksummed<?> other = (Checksummed<?>) obj;
    return unsignedChecksum == other.unsignedChecksum && Objects.equals(object, other.object);
  }

  @Override
  public int hashCode() {
    return Objects.hash(object, unsignedChecksum);
  }

  @Override
  public String toString() {
    return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  }

  public static short checksum(short... a) {
    return (short) CommonThings.sum(a);
  }
}
